

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>bbox.metrics &mdash; bbox 0.8.3 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> bbox
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../bbox.html">bbox package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">bbox</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>bbox.metrics</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for bbox.metrics</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Functions for metrics related to 2D and 3D bounding boxes.&quot;&quot;&quot;</span>

<span class="c1"># pylint: disable=invalid-name,missing-docstring,assignment-from-no-return,logging-format-interpolation</span>

<span class="kn">import</span> <span class="nn">logging</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">bbox</span> <span class="k">import</span> <span class="n">BBox2D</span><span class="p">,</span> <span class="n">BBox2DList</span><span class="p">,</span> <span class="n">BBox3D</span>
<span class="kn">from</span> <span class="nn">bbox.geometry</span> <span class="k">import</span> <span class="n">polygon_area</span><span class="p">,</span> <span class="n">polygon_collision</span><span class="p">,</span> <span class="n">polygon_intersection</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="iou_2d"><a class="viewcode-back" href="../../bbox.html#bbox.metrics.iou_2d">[docs]</a><span class="k">def</span> <span class="nf">iou_2d</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">BBox2D</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">BBox2D</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the Intersection over Union (IoU) of a pair of 2D bounding boxes.</span>

<span class="sd">    Alias for `jaccard_index_2d`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">jaccard_index_2d</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></div>


<div class="viewcode-block" id="jaccard_index_2d"><a class="viewcode-back" href="../../bbox.html#bbox.metrics.jaccard_index_2d">[docs]</a><span class="k">def</span> <span class="nf">jaccard_index_2d</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">BBox2D</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">BBox2D</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the Jaccard Index / Intersection over Union (IoU) of a pair of 2D bounding boxes.</span>

<span class="sd">    Args:</span>
<span class="sd">        a (:py:class:`BBox2D`): 2D bounding box.</span>
<span class="sd">        b (:py:class:`BBox2D`): 2D bounding box.</span>

<span class="sd">    Returns:</span>
<span class="sd">        :py:class:`float`: The IoU of the 2 bounding boxes.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">xA</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">x1</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x1</span><span class="p">)</span>
    <span class="n">yA</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">y1</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">y1</span><span class="p">)</span>
    <span class="n">xB</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">x2</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x2</span><span class="p">)</span>
    <span class="n">yB</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">y2</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">y2</span><span class="p">)</span>

    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;xA=</span><span class="si">{0}</span><span class="s2"> yA=</span><span class="si">{1}</span><span class="s2"> xB=</span><span class="si">{2}</span><span class="s2"> yB=</span><span class="si">{3}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">xA</span><span class="p">,</span> <span class="n">yA</span><span class="p">,</span> <span class="n">xB</span><span class="p">,</span> <span class="n">yB</span><span class="p">))</span>

    <span class="n">inter_w</span> <span class="o">=</span> <span class="n">xB</span> <span class="o">-</span> <span class="n">xA</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">inter_w</span> <span class="o">=</span> <span class="n">inter_w</span> <span class="o">*</span> <span class="p">(</span><span class="n">inter_w</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>

    <span class="n">inter_h</span> <span class="o">=</span> <span class="n">yB</span> <span class="o">-</span> <span class="n">yA</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">inter_h</span> <span class="o">=</span> <span class="n">inter_h</span> <span class="o">*</span> <span class="p">(</span><span class="n">inter_h</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>

    <span class="n">intersection</span> <span class="o">=</span> <span class="n">inter_w</span> <span class="o">*</span> <span class="n">inter_h</span>

    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;jaccard_index: intersection=</span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">intersection</span><span class="p">))</span>

    <span class="n">a_area</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">width</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">height</span>
    <span class="n">b_area</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">width</span> <span class="o">*</span> <span class="n">b</span><span class="o">.</span><span class="n">height</span>

    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;jaccard_index: a_area: </span><span class="si">{0}</span><span class="s2">, b_area: </span><span class="si">{1}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">a_area</span><span class="p">,</span> <span class="n">b_area</span><span class="p">))</span>

    <span class="n">iou</span> <span class="o">=</span> <span class="n">intersection</span> <span class="o">/</span> <span class="p">(</span><span class="n">a_area</span> <span class="o">+</span> <span class="n">b_area</span> <span class="o">-</span> <span class="n">intersection</span><span class="p">)</span>

    <span class="c1"># set nan and +/- inf to 0</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isinf</span><span class="p">(</span><span class="n">iou</span><span class="p">)</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">iou</span><span class="p">):</span>
        <span class="n">iou</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">return</span> <span class="n">iou</span></div>


<div class="viewcode-block" id="multi_iou_2d"><a class="viewcode-back" href="../../bbox.html#bbox.metrics.multi_iou_2d">[docs]</a><span class="k">def</span> <span class="nf">multi_iou_2d</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">BBox2DList</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">BBox2DList</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the Intersection over Union (IoU) of two sets of 2D bounding boxes.</span>

<span class="sd">    Alias for `multi_jaccard_index_2d`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">multi_jaccard_index_2d</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></div>


<div class="viewcode-block" id="multi_jaccard_index_2d"><a class="viewcode-back" href="../../bbox.html#bbox.metrics.multi_jaccard_index_2d">[docs]</a><span class="k">def</span> <span class="nf">multi_jaccard_index_2d</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">BBox2DList</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">BBox2DList</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the Jaccard Index (Intersection over Union) of two sets of 2D bounding boxes.</span>

<span class="sd">    Args:</span>
<span class="sd">        a (:py:class:`BBox2DList`): List of 2D bounding boxes.</span>
<span class="sd">        b (:py:class:`BBox2DList`): List of 2D bounding boxes.</span>

<span class="sd">    Returns:</span>
<span class="sd">        :py:class:`ndarray`: IoU Matrix</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># We need to add a trailing dimension so that max/min gives us a (N,N) matrix</span>
    <span class="n">xA</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">x1</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">b</span><span class="o">.</span><span class="n">x1</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
    <span class="n">yA</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">y1</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">b</span><span class="o">.</span><span class="n">y1</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
    <span class="n">xB</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">x2</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">b</span><span class="o">.</span><span class="n">x2</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
    <span class="n">yB</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">y2</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">b</span><span class="o">.</span><span class="n">y2</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>

    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">multi_jaccard_index:</span><span class="se">\n</span><span class="s2">xA</span><span class="se">\n</span><span class="si">{0}</span><span class="se">\n</span><span class="s2">yA</span><span class="se">\n</span><span class="si">{1}</span><span class="se">\n</span><span class="s2">xB</span><span class="se">\n</span><span class="si">{2}</span><span class="se">\n</span><span class="s2">yB</span><span class="se">\n</span><span class="si">{3}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
        <span class="n">xA</span><span class="p">,</span> <span class="n">yA</span><span class="p">,</span> <span class="n">xB</span><span class="p">,</span> <span class="n">yB</span><span class="p">))</span>

    <span class="n">inter_w</span> <span class="o">=</span> <span class="n">xB</span> <span class="o">-</span> <span class="n">xA</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">inter_w</span><span class="p">[</span><span class="n">inter_w</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="n">inter_h</span> <span class="o">=</span> <span class="n">yB</span> <span class="o">-</span> <span class="n">yA</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">inter_h</span><span class="p">[</span><span class="n">inter_h</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="c1"># maximum generates a (N,N) matrix which consumes a lot of memory</span>
    <span class="c1"># thus we are aggressive about freeing memory up.</span>
    <span class="k">del</span> <span class="n">xA</span>
    <span class="k">del</span> <span class="n">yA</span>
    <span class="k">del</span> <span class="n">xB</span>
    <span class="k">del</span> <span class="n">yB</span>

    <span class="n">intersection</span> <span class="o">=</span> <span class="n">inter_w</span> <span class="o">*</span> <span class="n">inter_h</span>
    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
        <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">multi_jaccard_index intersection:</span><span class="se">\n</span><span class="s2"> </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">intersection</span><span class="p">))</span>

    <span class="n">a_area</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">width</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">height</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span>
    <span class="n">b_area</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">width</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="n">b</span><span class="o">.</span><span class="n">height</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span>
    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
        <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">multi_jaccard_index:</span><span class="se">\n</span><span class="s2"> a_area:</span><span class="se">\n</span><span class="s2"> </span><span class="si">{0}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">b_area:</span><span class="se">\n</span><span class="s2"> </span><span class="si">{1}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">a_area</span><span class="p">,</span> <span class="n">b_area</span><span class="p">))</span>

    <span class="n">iou</span> <span class="o">=</span> <span class="n">intersection</span> <span class="o">/</span> <span class="p">(</span><span class="n">a_area</span> <span class="o">+</span> <span class="n">b_area</span><span class="o">.</span><span class="n">T</span> <span class="o">-</span> <span class="n">intersection</span><span class="p">)</span>

    <span class="c1"># set nan and +/- inf to 0</span>
    <span class="n">iou</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">isinf</span><span class="p">(</span><span class="n">iou</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">iou</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">iou</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">return</span> <span class="n">iou</span></div>

<div class="viewcode-block" id="iou_3d"><a class="viewcode-back" href="../../bbox.html#bbox.metrics.iou_3d">[docs]</a><span class="k">def</span> <span class="nf">iou_3d</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">BBox3D</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">BBox3D</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the Intersection over Union (IoU) of a pair of 3D bounding boxes.</span>

<span class="sd">    Alias for `jaccard_index_3d`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">jaccard_index_3d</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></div>


<div class="viewcode-block" id="jaccard_index_3d"><a class="viewcode-back" href="../../bbox.html#bbox.metrics.jaccard_index_3d">[docs]</a><span class="k">def</span> <span class="nf">jaccard_index_3d</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">BBox3D</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">BBox3D</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the Jaccard Index / Intersection over Union (IoU) of a pair of 3D bounding boxes.</span>
<span class="sd">    We compute the IoU using the top-down bird&#39;s eye view of the boxes.</span>

<span class="sd">    **Note**: We follow the KITTI format and assume only yaw rotations (along z-axis).</span>

<span class="sd">    Args:</span>
<span class="sd">        a (:py:class:`BBox3D`): 3D bounding box.</span>
<span class="sd">        b (:py:class:`BBox3D`): 3D bounding box.</span>

<span class="sd">    Returns:</span>
<span class="sd">        :py:class:`float`: The IoU of the 2 bounding boxes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># check if the two boxes don&#39;t overlap</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">polygon_collision</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">],</span> <span class="n">b</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]):</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">round_</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>

    <span class="n">intersection_points</span> <span class="o">=</span> <span class="n">polygon_intersection</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">],</span> <span class="n">b</span><span class="o">.</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">])</span>
    <span class="n">inter_area</span> <span class="o">=</span> <span class="n">polygon_area</span><span class="p">(</span><span class="n">intersection_points</span><span class="p">)</span>

    <span class="n">zmax</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">cz</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">cz</span><span class="p">)</span>
    <span class="n">zmin</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">cz</span> <span class="o">-</span> <span class="n">a</span><span class="o">.</span><span class="n">h</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">cz</span> <span class="o">-</span> <span class="n">b</span><span class="o">.</span><span class="n">h</span><span class="p">)</span>

    <span class="n">inter_vol</span> <span class="o">=</span> <span class="n">inter_area</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">zmax</span><span class="o">-</span><span class="n">zmin</span><span class="p">)</span>

    <span class="n">a_vol</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">l</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">w</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">h</span>
    <span class="n">b_vol</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">l</span> <span class="o">*</span> <span class="n">b</span><span class="o">.</span><span class="n">w</span> <span class="o">*</span> <span class="n">b</span><span class="o">.</span><span class="n">h</span>

    <span class="n">union_vol</span> <span class="o">=</span> <span class="p">(</span><span class="n">a_vol</span> <span class="o">+</span> <span class="n">b_vol</span> <span class="o">-</span> <span class="n">inter_vol</span><span class="p">)</span>

    <span class="n">iou</span> <span class="o">=</span> <span class="n">inter_vol</span> <span class="o">/</span> <span class="n">union_vol</span>

    <span class="c1"># set nan and +/- inf to 0</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isinf</span><span class="p">(</span><span class="n">iou</span><span class="p">)</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">iou</span><span class="p">):</span>
        <span class="n">iou</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">round_</span><span class="p">(</span><span class="n">iou</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Varun Agrawal

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>